﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;

namespace CamHelper.Interpreter
{
    public class ParameterFileLine : IParameterFileLine
    {
        public IParameterFile File { get; private set; }
        public int LineNumber { get; private set; }
        public ParameterFileLineType LineType { get; private set; }
        public string PureContent { get; private set; }
        protected string _lowerePureContent;

        private string _category;
        public string Category
        {
            get { return _category; }
            set
            {
                _category = value;
                CategoryLower = _category.ToLower();
            }
        }
        public string CategoryLower { get; private set; }
        public string Comment { get; private set; }

        public string Key { get; private set; }
        public string KeyLower { get; private set; }
        public string Value { get; private set; }
        public bool EmptyValue { get; private set; }
        public char ValueType { get; private set; }
        public bool IsLogicalAnd { get; private set; }

        #region Constructor

        public ParameterFileLine(IParameterFile file, int lineNumber, string content)
        {
            if (lineNumber < 0)
                throw new ArgumentOutOfRangeException("lineNumber");
            if (content == null)
                throw new ArgumentNullException("content");

            File = file;
            LineNumber = lineNumber;
            LineType = ParameterFileLineType.Empty;
            PureContent = content;
            _lowerePureContent = PureContent.ToLower();
            Category = String.Empty;
            Comment = String.Empty;
            Key = String.Empty;
            KeyLower = String.Empty;
            Value = String.Empty;
            EmptyValue = true;
            ValueType = Char.MinValue;
            IsLogicalAnd = false;

            if (PureContent.StartsWith("["))
            {
                LineType = ParameterFileLineType.Category;
                int pos = PureContent.IndexOf(']');
                Category = PureContent.Substring(1, pos - 1);
                pos = PureContent.IndexOf("//", pos);
                if (pos != -1)
                {
                    Comment = PureContent.Substring(pos + 3);
                }
            }
            else if (PureContent.StartsWith("#"))
            {
                LineType = ParameterFileLineType.KeyValue;

                int pos = PureContent.IndexOf('=');
                if (PureContent[1] != ' ')
                    Key = PureContent.Substring(1, pos - 1).Trim();
                else
                    Key = PureContent.Substring(2, pos - 2).Trim();
                KeyLower = Key.ToLower();

                int posStart = PureContent.IndexOf('=') + 2;
                int posComment = (PureContent.Length > posStart ? PureContent.IndexOf("//", posStart) : -1);
                int posEndValue = PureContent.Length - posStart;

                if (posComment != -1)
                {
                    posEndValue = posComment - posStart;
                    if (PureContent.Length > posComment + 3)
                        Comment = PureContent.Substring(posComment + 3);
                }

                Value = PureContent.Length > posStart ? PureContent.Substring(posStart, posEndValue).Trim() : String.Empty;
                if (Value.Length > 1 && Value[1] == ',')
                {
                    ValueType = Value[0];
                    if (Value.Length > 3 && Value.Substring(1, 3) == ",&,")
                    {
                        IsLogicalAnd = true;
                        Value = Value.Substring(4);
                    }
                    else
                        Value = Value.Substring(2);
                }

                EmptyValue = (Value.Length == 0);
            }
        }

        #endregion

        public override string ToString()
        {
            string s = String.Format("{0}: {1}{2}", LineNumber, PureContent, Environment.NewLine);
            s += String.Format("FileName: {0}, LineType: {1}, Category: {2}{3}", File.FileName, LineType, Category, Environment.NewLine);
            s += String.Format("Key: {0}, ValueType: {1}, EmptyValue: {2}{3}", Key, ValueType, EmptyValue, Environment.NewLine);
            s += String.Format("Value: {0}, Comment: {1}{3}", Value, Comment, Environment.NewLine);
            return s;
        }
    }
}
